//
//  HDIconTextFieldView.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/12.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDIconTextFieldView: UIView {

    let getCodeButton = UIButton()
    let icon = UIImageView()
    let textField = UITextField()
    
    var timer = Timer()
    var timeCount = 60
    init() {
        super.init(frame: CGRect.zero)
        
        self.backgroundColor = UIColor.white
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupViews() {
        
        let line1 = UILabel(frame: CGRect(x: 0,y: 0,width: AppConstant.screenWidth,height: 0.4))
        line1.backgroundColor = UIColor(hex: 0xdbdbdb)
        self.addSubview(line1)
        let line2 = UILabel()
        line2.backgroundColor = UIColor(hex: 0xdbdbdb)
        self.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(0.4)
        }
        
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(11)
            make.width.height.equalTo(23)
        }
    
        
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = UIColor(hex: 0x999999)
        line.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(icon.snp.right).offset(10)
            make.width.equalTo(1)
            make.height.equalTo(18)
        }
        
        
        self.addSubview(textField)
        textField.textColor = UIColor(hex: 0x333333)
        textField.font = UIFont.systemFont(ofSize: 15)
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(line.snp.right).offset(12)
            make.centerY.equalTo(self)
            make.top.bottom.equalTo(self)
            make.right.equalTo(-10)
        }
        
        self.addSubview(getCodeButton)
        getCodeButton.setTitle("获取验证码", for: UIControlState())
        getCodeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        getCodeButton.setTitleColor(UIColor(hex: 0x999999), for: UIControlState())
        getCodeButton.isHidden = true
        getCodeButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.right.equalTo(self).offset(-10)
            make.top.bottom.equalTo(self)
            make.width.equalTo(85)
        }

    }
    
    
    
    func startTimer() {
        getCodeButton.isEnabled = false
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(HDRegisterCell.beginCountDown), userInfo: nil, repeats: true)
    }
    
    func beginCountDown() {
        print(timeCount)
        timeCount -= 1
        
        getCodeButton.setTitle("(\(timeCount)秒)", for: .disabled)
        if timeCount == 0 {
            getCodeButton.isEnabled = true
            timer.invalidate()
            timeCount = 60
        }
        
        
    }
    
    
}
